﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Orange.Mvc4App.Models;

namespace Orange.Mvc4App.Services {

    public interface IStoreService : IBaseService<Store> {
        Store GetNext();
    }

    public class StoreService : BaseService, IStoreService {
        public IList<Store> GetList(Func<Store, bool> filters) {
            if (filters != null)
                return db.Stores.Where(filters).ToList();
            else
                return db.Stores.ToList();
        }

        public Store Find(params object[] keys) {
            return db.Stores.Find(keys);
        }

        public void Add(Store model) {
            db.Stores.Add(model);
        }

        public void Update(Store model) {
            db.Entry(model).State = EntityState.Modified;
        }

        public void Delete(Store model) {
            db.Stores.Remove(model);
        }

        public void Save() {
            db.SaveChanges();
        }

        public void Dispose() {
            db.Dispose();
        }

        public Store GetNext() {
            int id = 1;
            if (db.Stores.Count() > 0)
                id = db.Stores.Max(c => c.Storeid) + 1;
            return new Store { Storeid = id };
        }
    }
}